//
//  CPModal.swift
//  字典转模型
//
//  Created by chen on 15/3/12.
//  Copyright (c) 2015年 chen. All rights reserved.
//
import UIKit

// protocol KeyValue{
//    static func objectWithDiction(dic: NSDictionary) -> AnyObject?
//    static func typeMap() -> [String: String]?
//   
//}

extension NSObject{
    // 实现协议的方法
    class func objectWithDiction(dic: NSDictionary) -> AnyObject? {
        let infoForClass = self.allinfoType()
        
        let model  = self.alloc()
        
        if infoForClass == [:]{
            return nil
        }
        
        
        for (k , typeName) in infoForClass {
            
            if (typeName == "") { // 系统属性
                // 判断 字典是不是有 key
                if let value: AnyObject = dic.objectForKey(k){
                    model.setValue(value, forKey: k)
                }
                
            } else{ // 字典 或者是 数组 --> 字典， 数组
                
                if let value:AnyObject = dic.objectForKey(k){
                    
                    if value.isKindOfClass(NSArray.self) {
                        var obj: AnyObject?
                        var objArray = [AnyObject]()
                        if let objClass: AnyClass = NSClassFromString(typeName){
                            //  遍历字典
                            for  arrayValue in value as! NSArray{
                                
                                obj = objClass.objectWithDiction(arrayValue as! NSDictionary)
                                
                                if obj != nil{
                                    objArray.append(obj!)
                                    
                                }
                                
                                
                            }
                            if objArray.count > 0 {
                               model.setValue(objArray, forKey: k)
                            }
                        }
                        
                        
                       
                        
                    } else if value.isKindOfClass(NSDictionary.self){
                      //  是字典
                       
                        if let subClass: AnyClass = NSClassFromString(typeName){
                            
                            if let subObj: AnyObject? =  subClass.objectWithDiction(value as! NSDictionary) {
                                
                                model.setValue(subObj, forKey:k)
                            }
                            
                            
                            
                        }


                        
                        
                    }
                
                }
            }
            
            
        }
        
         return model
    }
    
    ///  modal 中描述自定义， 数组的类型
    ///   子类中 最好 重载
    ///  :returns: 一个字典： 其中k --》为属性名， V-》 类型名
    class func typeMap() -> [String: String]?{
        return nil
    }
    
    class func allinfoType() -> [String: String]{
        
        var classDic = [String: String]()
        
        var currentClass = self
        
        while var parenter: AnyClass? = currentClass.superclass() {
            
            if (parenter == nil) {
                break
            }
            
            if let dic = currentClass.InfoType() {
                classDic.merge(dic)
            }
            
            currentClass = parenter! as! NSObject.Type
        
        }
        
       return classDic
    }
    
    class func InfoType() -> [String: String]?{
        
        
        var dic: [String: String]? = typeMap()
        if dic == nil {
            
            dic = [String: String]()
            
        }
        // 得到类型：----》 得到各种的 属性名：类型
        var cout:UInt32 = 0
        let ivars = class_copyIvarList(self, &cout)
        for i in 0..<cout{
            let ivar = ivars[Int(i)]
            let cpropertyName = ivar_getName(ivar)
            let stringName = String.fromCString(cpropertyName)
            // 属性名： ---》知道 属性名是不是 字典， 还是数组
            if  let hasKey = dic?.indexForKey(stringName!) {
                // 说明是自己定义的：
                continue
            }
            dic!.updateValue("", forKey: stringName!)
           
        }
       
        return dic
    }
    
}

extension Dictionary {
    mutating func merge(dic: [String: String]) -> (){
        
         for (k,v) in dic {
           self.updateValue(v as! Value, forKey: k as! Key)
            
        }
        
    }
    
}

